package com.ygy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ygy.entity.Like;
import com.ygy.service.ICommentsService;
import com.ygy.service.LikeService;
import com.ygy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/like")
public class LikeController {

    @Autowired
    private LikeService likeService;

    @PostMapping("/like")
    public Result likeControl(@RequestBody Like like){

        //检查用户是否登录  : session 中 ，登录的用户才能点赞，否则返回提示信息，暂时不做

        //查询 该用户 对 该条评论 是否点赞过，如果点赞过则取消点赞，没有则增加点赞
        String commentId = like.getCommentId();
        Integer userId = (Integer)like.getUserId();

        QueryWrapper<Like> wrapper = new QueryWrapper<>();
        wrapper.eq("comment_id",commentId);
        wrapper.eq("user_id",userId);

        //构造条件获取一条我的点赞记录
        Like tLike = likeService.getOne(wrapper);

        if (tLike==null){
            // 没有点赞，则点赞+1
            likeService.save(like);
        }else {
            // 点过赞，点赞-1
            likeService.removeById(tLike.getId());
        }

        return Result.success();
    }
}
